﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Manager : MonoBehaviour {

    public static Manager Instance;
    Canvas mainUI, failedUI;

    Text m_scoreText, m_lifeText, m_highestScoreText;
    int m_score = 0, m_life = 0, m_highestScore = 0;
    Player player;
   public AudioClip m_bkMusic;
    AudioSource m_bkMusicSource;


	// Use this for initialization
	void Start () {
        Instance = this;
        //m_bkMusic=ass
        mainUI = GameObject.Find("mainUI").GetComponent<Canvas>();
        failedUI = GameObject.Find("failedUI").GetComponent<Canvas>();
        m_scoreText = mainUI.transform.Find("score").GetComponent<Text>();
        m_lifeText = mainUI.transform.Find("life").GetComponent<Text>();
        m_highestScoreText = mainUI.transform.Find("highstscore").GetComponent<Text>();
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        m_bkMusicSource = gameObject.AddComponent<AudioSource>();
        m_bkMusicSource.clip = m_bkMusic;
        m_bkMusicSource.loop = true;

        m_scoreText.text = string.Format("分数：{0}",m_score);
        m_lifeText.text = string.Format("生命值：{0}", player.m_life);
        m_highestScoreText.text = string.Format("最高分数：{0}", m_highestScore);
        failedUI.gameObject.SetActive(false);

        var button = failedUI.transform.Find("Button").GetComponent<Button>();

        button.onClick.AddListener(delegate(){
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
	    });
    }

    public void addScore(int point)
    {
        m_score += point;
        if (m_highestScore<m_score)
        {
            m_highestScore = m_score;

        }
        m_scoreText.text = string.Format("分数：{0}", m_score);
        m_highestScoreText.text = string.Format("最高分数：{0}", m_highestScore);

    }

    public void changeLife(int life)
    {
        m_lifeText.text = string.Format("生命值：{0}", life);
        if (life<=0)
        {
            failedUI.gameObject.SetActive(true);

        }

    }

    // Update is called once per frame
    void Update () {
		
	}
}
